<?php
/* 
 * This file is part of the isicsBreadcrumbsPlugin package.
 * 
 * Copyright (C) 2007-2008 ISICS.fr <contact@isics.fr>
 * 
 * isicsBreadcrumbsPlugin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * isicsBreadcrumbsPlugin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with isicsBreadcrumbsPlugin.  If not, see <http://www.gnu.org/licenses/>.
 */

class isicsBreadcrumbsItem
{
  protected $text;
  protected $uri;
    
  /**
   * Constructor
   *
   */    
  public function __construct($text, $uri = null)
  {
    $this->text = $text;
    $this->uri  = $uri;
  }
  
  /**
   * Retrieve the uri of the item
   *
   */  
  public function getUri()
  {
    return $this->uri;
  }
  
  /**
   * Retrieve the text of the item
   *
   */  
  public function getText()
  {
    return $this->text;
  }
}

class isicsBreadcrumbs
{
  static    $instance = null;
  protected $items    = array();
  
  
  /**
   * Constructor
   *
   */
  public function __construct()
  {
    $this->setRoot('Home', '@homepage');
  }

  /**
   * Add an item
   *
   * @param string $text
   * @param string $uri
   */
  public function addItem($text, $uri = null)
  {
    array_push($this->items, new isicsBreadcrumbsItem($text, $uri));
    $this->save();
  }

  /**
   * Delete all existings items
   *
   */
  public function clearItems()
  {
    $this->items = array();
    $this->save();
  }  
  
  /**
   * Get the unique isicsBreadcrumbs instance (singleton)
   *
   */
  public static function getInstance()
  {
    if (is_null(self::$instance))
    {
      if (sfContext::getInstance()->getResponse()->hasParameter('isicsBreadcrumbs'))
      {
        self::$instance = sfContext::getInstance()->getResponse()->getParameter('isicsBreadcrumbs');
      }
      else
      {
        self::$instance = new isicsBreadcrumbs();
        self::$instance->save();
      }
    }
    
    return self::$instance;
  }
  
  
  /**
   * Retrieve an array of isicsBreadcrumbsItem
   *
   * @param int $offset
   */
  public function getItems($offset = 0)
  {
    return array_slice($this->items, $offset);
  }
  
  /**
   * Redefine the root item
   *
   */
  public function setRoot($text, $uri)
  {
    $this->items[0] = new isicsBreadcrumbsItem($text, $uri);
    $this->save();
  }
  
  /**
   * Save isicsBreadcrumbs instance as response parameter (allows action caching)
   *
   */
  protected function save()
  {
    sfContext::getInstance()->getResponse()->setParameter('isicsBreadcrumbs', $this);
  }
}